package com.zmj.sy.mom.srv.aps.service;

import com.zmj.sy.mom.srv.aps.bean.entity.base.GroupAlternative;
import com.zmj.sy.mom.srv.aps.bean.entity.resource.Group;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseIdReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseListResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.groupalternative.GroupAlternativeAddReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.groupalternative.GroupAlternativeListReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.groupalternative.GroupAlternativeListResVo;
import com.zmj.sy.mom.srv.aps.mapper.GroupAlternativeMapper;
import com.zmj.sy.mom.srv.aps.mapper.GroupMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
public class GroupAlternativeService {

    private final GroupAlternativeMapper groupAlternativeMapper;

    private final GroupMapper groupMapper;

    @Transactional(readOnly = true)
    public BaseListResVo<GroupAlternativeListResVo> list(GroupAlternativeListReqVo reqVo) {

        if(reqVo.getGroupId() == null && reqVo.getGroupCode() == null){
            List<Group> groupList = groupMapper.lambdaQuery()
                    .isNotNull(Group::getMyCode)
                    .eq(Group::getType, 2)
                    .list();

            List<GroupAlternative> list = groupList.stream()
                    .map(e -> {
                        GroupAlternative ba = new GroupAlternative();
                        ba.setId(e.getId());
                        ba.setGroupCode(e.getMyCode());
                        ba.setGroupName(e.getName());
                        return ba;
                    })
                    .sorted(Comparator.comparing(GroupAlternative::getGroupCode))
                    .collect(Collectors.toList());
            return BaseListResVo.of(list, GroupAlternativeListResVo.class);

        }

        Group group = groupMapper.lambdaQuery()
                .eq(Group::getType, 2)
                .eq(reqVo.getGroupId() != null, Group::getId, reqVo.getGroupId())
                .eq(reqVo.getGroupCode() != null, Group::getMyCode, reqVo.getGroupCode())
                .one();

        GroupAlternative groupAlternative = groupAlternativeMapper.lambdaQuery().eq(GroupAlternative::getGroupId, group.getId()).one();

        if(groupAlternative == null){
            return BaseListResVo.empty();
        }
        List<GroupAlternative> list = groupAlternativeMapper.lambdaQuery().eq(GroupAlternative::getBatchId, groupAlternative.getBatchId()).list();
        list.sort(Comparator.comparing(GroupAlternative::getGroupCode));
        return BaseListResVo.of(list, GroupAlternativeListResVo.class);

    }

    @Transactional
    public void add(GroupAlternativeAddReqVo reqVo) {
        GroupAlternative append = groupAlternativeMapper.lambdaQuery().eq(GroupAlternative::getGroupId, reqVo.getAppendGroupId()).one();
        if(append != null){
            return ;
        }

        GroupAlternative source = groupAlternativeMapper.lambdaQuery().eq(GroupAlternative::getGroupId, reqVo.getGroupId()).one();
        if(source == null){
            Group group = groupMapper.selectById(reqVo.getGroupId());
            GroupAlternative append1 = new GroupAlternative();
            append1.setGroupId(group.getId());
            append1.setGroupCode(group.getMyCode());
            append1.setGroupName(group.getName());
            append1.setBatchId(reqVo.getGroupId());
            groupAlternativeMapper.insert(append1);
        }

        Group group1 = groupMapper.selectById(reqVo.getAppendGroupId());
        GroupAlternative append1 = new GroupAlternative();
        append1.setGroupId(group1.getId());
        append1.setGroupCode(group1.getMyCode());
        append1.setGroupName(group1.getName());
        append1.setBatchId(reqVo.getGroupId());
        groupAlternativeMapper.insert(append1);
    }

    @Transactional
    public void delete(BaseIdReqVo reqVo) {
        GroupAlternative groupAlternative = groupAlternativeMapper.selectById(reqVo.getId());
        groupAlternativeMapper.deleteById(reqVo.getId());

        Integer count = groupAlternativeMapper.lambdaQuery().eq(GroupAlternative::getBatchId, groupAlternative.getBatchId()).count();
        if(count.equals(1)){
            groupAlternativeMapper.lambdaUpdate().eq(GroupAlternative::getBatchId, groupAlternative.getBatchId()).remove();
        }
    }
}
